package cn.iocoder.yudao.module.pay.service.pricingrules;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.pay.controller.admin.pricingrules.vo.*;
import cn.iocoder.yudao.module.pay.dal.dataobject.pricingrules.PricingRulesDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.pay.dal.mysql.pricingrules.PricingRulesMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.pay.enums.ErrorCodeConstants.*;

/**
 * 用于支付模型翻译的价格套餐列 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PricingRulesServiceImpl implements PricingRulesService {

    @Resource
    private PricingRulesMapper pricingRulesMapper;

    @Override
    public Integer createPricingRules(PricingRulesSaveReqVO createReqVO) {
        // 插入
        PricingRulesDO pricingRules = BeanUtils.toBean(createReqVO, PricingRulesDO.class);
        pricingRulesMapper.insert(pricingRules);
        // 返回
        return pricingRules.getId();
    }

    @Override
    public void updatePricingRules(PricingRulesSaveReqVO updateReqVO) {
        // 校验存在
        validatePricingRulesExists(updateReqVO.getId());
        // 更新
        PricingRulesDO updateObj = BeanUtils.toBean(updateReqVO, PricingRulesDO.class);
        pricingRulesMapper.updateById(updateObj);
    }

    @Override
    public void deletePricingRules(Integer id) {
        // 校验存在
        validatePricingRulesExists(id);
        // 删除
        pricingRulesMapper.deleteById(id);
    }

    private void validatePricingRulesExists(Integer id) {
        if (pricingRulesMapper.selectById(id) == null) {
            throw exception(PRICING_RULES_NOT_EXISTS);
        }
    }

    @Override
    public PricingRulesDO getPricingRules(Integer id) {
        return pricingRulesMapper.selectById(id);
    }

    @Override
    public PageResult<PricingRulesDO> getPricingRulesPage(PricingRulesPageReqVO pageReqVO) {
        return pricingRulesMapper.selectPage(pageReqVO);
    }

}